package gulimall.service.impl;

import gulimall.entity.SpuImages;
import gulimall.mapper.SpuImagesMapper;
import gulimall.service.SpuImagesService;
import gulimall.utils.PageUtils;
import gulimall.utils.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


@Service("spuImagesService")
public class SpuImagesServiceImpl extends ServiceImpl<SpuImagesMapper, SpuImages> implements SpuImagesService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuImages> page = this.page(
                new Query<SpuImages>().getPage(params),
                new QueryWrapper<SpuImages>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存spu图片集
     *
     * @param spuId
     * @param images
     */
    @Override
    public void saveImages(Long spuId, List<String> images) {
        List<SpuImages> imagesList = images.stream().map(img -> {
            SpuImages spuImages = new SpuImages();
            spuImages.setImgUrl(img);
            spuImages.setSpuId(spuId);
            return spuImages;
        }).collect(Collectors.toList());
        this.saveBatch(imagesList);
    }

}